﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Category
{
    public partial class Form : FormPage
    {
        BLL.Category bll = new BLL.Category();
        Model.Category model = new Model.Category();
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { model = bll.GetModel(Master.id);}
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            tb_Name.Text = model.Name;
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            Model.Category modelFatherCategory=new Model.Category();
            int FatherCategoryID = DataTypePlus.requestInt(Request.QueryString["FatherCategoryID"]);
            if (FatherCategoryID > 0)modelFatherCategory = bll.GetModel(FatherCategoryID);
            //
            string Name = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Name.Text));

            if (
                Master.noEmpty(Name, "[分类名称]必须填写") &&
                Master.noChongfu("Name='" + Name + "' and FatherCategoryID=" + FatherCategoryID, "[奖品名称]重复")
                )
            {
                model.Name = Name;
                //Rank
                List<Model.Category> listModelCategory = bll.GetModelList("FatherCategoryID=" + FatherCategoryID + " order by Rank desc");
                if (listModelCategory != null && listModelCategory.Count > 0) model.Rank = listModelCategory[0].Rank + 1;
                else model.Rank = 1;
                //Number
                model.Number = model.Rank.ToString().PadLeft(2, '0');
                if (FatherCategoryID > 0) model.Number = modelFatherCategory.Number + model.Number;
                //
                if (Master.formState == EnumHelper.FormState.add)
                {
                    model.FatherCategoryID = FatherCategoryID;
                    model.CategoryID_Tree = "";
                    Master.id = bll.Add(model);
                    //更新idTree
                    model = bll.GetModel(Master.id);
                    model.CategoryID_Tree = Master.id + "|";
                    if (FatherCategoryID > 0) model.CategoryID_Tree = modelFatherCategory.CategoryID_Tree + model.CategoryID_Tree;
                    bll.Update(model);
                }
                else bll.Update(model);
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Goods_BuyPrice_Log, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
                Master.SubmitOK();

            }
        }
    }
}